<template>
	<div class="role" v-if="dialogFormVisible">
		<el-dialog :title="title" :visible="dialogFormVisible" :close-on-click-modal="false" width="800px" :before-close="handleClose">
			<div class="tab">
				<el-form ref="form" :rules="rules" :model="form" label-width="80px">
					<el-form-item label="产品线名" prop="name">
						<el-input v-model="form.name" placeholder="请输入产品线名" :disabled="num == 3"></el-input>
					</el-form-item>
					<el-form-item label="状态">
						<el-select v-model="form.status" placeholder="请选择状态" style="width:100%;" :disabled="num == 3">
							<el-option label="禁用" :value="0"></el-option>
							<el-option label="正常" :value="1"></el-option>
						</el-select>
					</el-form-item>
				</el-form>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
				<el-button type="primary" @click="add('form')" v-if="num != 3">提 交</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getHeader } from '@/app.config'
export default {
	name: 'addForm',
	data(){
		return {
			dialogFormVisible:false,
			form:{
				name:'',
				leader_spare:[],
				leader:[],
				status:1
			},
			id:'',
			rules:{
				name: [
					{max: 36, message: '产品线名不能超过36个字符', trigger: 'blur'},
					{ required: true, message: '请输入产品线名', trigger: 'blur' }
				],
			},
			optionsLeader:[],
			optionsSpare:[],
			testLeader:[],
			test_leader_spare:[],
			num:0,
			title:'新增'
		}
	},
	created() {

	},
	methods:{
		leaderChange(e){
			console.log('leaderChange',e)
		},
		spareChange(e){
			console.log('spareChange',e)
		},
		handleClose(){
			this.dialogFormVisible = false
			this.form = {}
		},
		add(form){
			this.$refs[form].validate((valid) => {
				if (valid) {
					if(this.num == 1){
						var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/product/line`;
						var data = this.form
						this.$axios.post(source,data,{headers: getHeader()})
							.then((response) => {
								if (response.status == 200) {
									this.dialogFormVisible = false
									this.$parent.initTable()
									this.form = {}
									this.$message.success("新增成功!")
								}
							}).catch((err) => {
							console.log(err);
						});
					}
				} else {
					return false;
				}
			});

		},
		getLeader(){
			var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/user/list?role=${'test_leader'}`;
			this.$axios.get(source,{headers: getHeader()})
				.then((response) => {
					if (response.status == 200) {
						this.optionsLeader = Object.assign({},response.data.data)
					}
				}).catch((err) => {
				console.log(err);
			});
		},
		getSpare(){
			var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/user/list`;
			this.$axios.get(source,{headers: getHeader()})
				.then((response) => {
					if (response.status == 200) {
						this.optionsSpare = Object.assign({},response.data.data)
					}
				}).catch((err) => {
				console.log(err);
			});
		},
		initData(num,data){
			this.num = num
			if(data){
				this.id = data.id
			}
			if(num == 1){
				this.getLeader()
				this.getSpare()
			}
			if(num == 3){
				this.title = '查看'
				this.form = Object.assign({},data)
				this.form.leader_spare = []
				this.form.leader =[]
				// data.leader_spare.filter(item => {
				// 	this.form.leader_spare.push(item.name)
				// })
				// data.leader.filter(item => {
				// 	this.form.leader.push(item.name)
				// })
			}
		}
	}

}
</script>

<style lang="scss" scoped>
.role{
	//.tab{
	//	width: 450px;
	//	margin: 0 0 0 40px;
	//}
}
</style>
